<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="utf-8">
    <title>Location</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
    <meta http-equiv="X-UA-Compatible" content="IE=Edge">
    <style>
    body,
    html,
    #container{
        overflow: hidden;
        width: 100%;
        height: 100%;
        margin: 0;
        font-family: "微软雅黑";
    }
    .info{
        z-index: 999;
        width: auto;
        min-width: 22rem;
        padding: .75rem 1.25rem;
        margin-left: 1.25rem;
        position: fixed;
        top: 1rem;
        background-color: #fff;
        border-radius: .25rem;
        font-size: 14px;
        color: #666;
        box-shadow: 0 2px 6px 0 rgba(27, 142, 236, 0.5);
    }
    </style>
    <script src="//api.map.baidu.com/api?type=webgl&v=1.0&ak=EzAgfFNdkPDSZxWfU8aoc516ZgcXj3MM"></script>
</head>
<body>

    <div id="container"></div>

    <script>
        // 初始化地图
        // var map = new BMap.Map("map");
        // map.enableScrollWheelZoom(true);
        lng=117.14648063708772;
        lat=39.06567016336429;
        var map = new BMapGL.Map('container'); // 创建Map实例
        map.centerAndZoom(new BMapGL.Point( lng,lat), 20); // 初始化地图,设置中心点坐标和地图级别
        map.enableScrollWheelZoom(true); // 开启鼠标滚轮缩放

        // 创建覆盖物
        // 创建点标记
        //var marker = new BMapGL.Marker(new BMapGL.Point(lng, lat));
        ////// 在地图上添加点标记
        //map.addOverlay(marker);
        //marker.remove();
        // var marker = new BMapGL.Marker(new BMapGL.Point(lng, lat));
        // var polyline = new BMap.Polyline([], {
        //     strokeColor: "#FF0000",
        //     strokeWeight: 3,
        //     strokeOpacity: 0.7
        // });
        // // map.addOverlay(marker);
        // map.addOverlay(polyline);

        // 轨迹点数组（最多保留50个点）
        var trackPoints = [];
        const MAX_POINTS = 50;

        function update() {
            fetch('gps_data.json')
                .then(res => {
                    if (!res.ok) throw new Error("HTTP error: " + res.status);
                    return res.json();
                })
                .then(data => {
                    console.log("new location:", data); 
                    // 更新坐标显示
                    //document.getElementById('lng').textContent = data.lng.toFixed(6);
                    //document.getElementById('lat').textContent = data.lat.toFixed(6);

                    // 创建点对象
                    //var point = new BMapGL.Point(data.lng, data.lat);
                    
                    // 更新标记
                    
                    var newPoint = new BMapGL.Point(data.lng, data.lat);
                    marker = new BMapGL.Marker(newPoint);
                    map.addOverlay(marker);
                    marker.remove();
                    map.setCenter(newPoint);
                    //marker.setPosition(point);
                    //map.panTo(point);

                    // 更新轨迹
                    // trackPoints.push(point);
                    // if (trackPoints.length > MAX_POINTS) trackPoints.shift();
                    // polyline.setPath(trackPoints);
                })
                .catch(err => console.error('数据获取失败:', err));
        }
        map.addEventListener('click', function (e) {
        alert('Longitude:' + e.latlng.lng + ',Latitude:' + e.latlng.lat);
    });
        // 每1秒更新一次
        setInterval(update, 1000);
        update(); // 立即执行第一次
    </script>
</body>
</html>
